﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.Classes
{
    public class HistoryAccessConsolidator
    {
        TuanAnhEntities db = new TuanAnhEntities();
        public void SaveChanges()
        {
            db.SaveChanges();
        }
        public IList<AccountList> getUserList()
        {
            IList<AccountList> list = new List<AccountList>();
          var data= db.vw_aspnet_Users.Where(e=>e.UserName!="superadmin").Select(e => e).ToList();
            foreach (var item in data)
            {
                AccountList model = new AccountList();
                model.user = item;
                model.islocked = islocked(item.UserId);
                list.Add(model);
            }
            return list;
        }
        public IList<AccountList> getUserListByUserName(string userName)
        {
            IList<AccountList> list = new List<AccountList>();
            var data = db.vw_aspnet_Users.Where(e => e.UserName.Contains(userName.ToUpper())).Select(e => e).ToList();
             foreach (var item in data)
            {
                AccountList model = new AccountList();
                model.user = item;
                model.islocked = islocked(item.UserId);
                list.Add(model);
            }
            return list;
        }
        public vw_aspnet_MembershipUsers getUserHistoryLog(Guid userID)
        {
            var data = db.vw_aspnet_MembershipUsers.Where(e => e.UserId == userID).Select(e => e);
            return data.Count() > 0 ? data.Single() : null;
        }
        public bool islocked(Guid userID)
        {
            var data = db.vw_aspnet_MembershipUsers.Where(e => e.UserId == userID).Select(e => e.IsLockedOut).Single();
            return data;
        }

        public aspnet_Users getUserInfo(Guid userID)
        {
            var info = db.aspnet_Users.Where(e => e.UserId == userID).Select(e => e);
            return info.Count() > 0 ? info.Single() : null;
        }

        public aspnet_Membership getMembershipInfo(Guid userID)
        {
            var info = db.aspnet_Membership.Where(e => e.UserId == userID).Select(e => e);
            return info.Count() > 0 ? info.Single() : null;
        }
        public vw_aspnet_MembershipUsers getViewMembershipUserInfo(Guid userID)
        {
            var info = db.vw_aspnet_MembershipUsers.Where(e => e.UserId == userID).Select(e => e);
            return info.Count() > 0 ? info.Single() : null;
        }

        public bool checkUserRolesInfo(Guid userID, Guid roleID)
        {
            var info = db.vw_aspnet_UsersInRoles.Where(e => e.UserId == userID && e.RoleId == roleID).Select(e => e);
            return info.Count() > 0 ? true : false;
        }

        public IList<vw_aspnet_Roles> getAllRoles()
        {
            var list = db.vw_aspnet_Roles.Select(e => e);
            return list.Count() > 0 ? list.ToList() : null;
        }
        public string Username(System.Guid userId)
        {
            string user = db.aspnet_Users.Where(e => e.UserId == userId).Select(e => e.UserName).First();
            return user;
        }
        public IList<aspnet_Roles> getRoleByDes(string roleDes)
        {
            var list = db.aspnet_Roles.Where(e => e.Description == roleDes).Select(e => e);
            return list.Count() > 0 ? list.ToList() : null;
        }
        public void updateUserInfo(Guid userID, aspnet_Users newUserInfo)
        {
            var info = getUserInfo(userID);
            if (info != null)
            {
                info.UserName = newUserInfo.UserName;
                info.LoweredUserName = newUserInfo.UserName.ToLower();
                info.MobileAlias = newUserInfo.MobileAlias;
                SaveChanges();
            }
        }

        public void updateMembershipInfo(Guid userID, aspnet_Membership newMemberShip)
        {
            var info = getMembershipInfo(userID);
            if (info != null)
            {
                info.Email = newMemberShip.Email;
                info.IsApproved = newMemberShip.IsApproved;
                info.Comment = newMemberShip.Comment;
                SaveChanges();
            }
        }

        public void updateUserInRole(AccountWrapper wrap)
        {
            var appID = wrap.MembershipUser.ApplicationId;
            var appName = db.aspnet_Applications.Where(e => e.ApplicationId == appID).Select(e => e.ApplicationName).Single();
            foreach (var group in wrap.userRoleGroupWrap)
            {
                foreach (var item in group.userRolesWrap)
                {
                    Guid roleID = Guid.Parse(item.roleID);
                    Guid userID = Guid.Parse(item.userID);
                    var roleName = db.aspnet_Roles.Where(e => e.RoleId == roleID).Select(e => e.RoleName).Single();
                    var userName = db.aspnet_Users.Where(e => e.UserId == userID).Select(e => e.UserName).Single();
                    var data = db.vw_aspnet_UsersInRoles.Where(e => e.RoleId == roleID && e.UserId == userID).Select(e => e);
                    if (data.Count() > 0)
                    {
                        if (!item.check)
                        {
                            db.aspnet_UsersInRoles_RemoveUsersFromRoles(appName, userName, roleName);
                            SaveChanges();
                        }
                    }
                    else
                    {
                        if (item.check)
                        {
                            db.aspnet_UsersInRoles_AddUsersToRoles(appName, userName, roleName, DateTime.UtcNow);
                            SaveChanges();
                        }
                    }
                }
            }
        }
        public bool checkEmail(string email)
        {
            var check = db.aspnet_Membership.Where(e => e.Email.ToLower() == email.ToLower()).Select(e => e);
            if (check.Count() > 0)
                return true;
            else
                return false;
        }
        public IList<string> getRoleGroup()
        {
            var group = db.aspnet_Roles.Select(e => e.Description).Distinct().ToList();
            return group;
        }
        #region DELETE
        public void Delete_UserInRole(Guid userID)
        {
            db.SP_DELETE_USER_IN_ROLE(userID);
        }
        public void Delete_MemberShip(Guid userID)
        {
            aspnet_Membership member = db.aspnet_Membership.Where(e => e.UserId == userID).Select(e => e).First();
            db.aspnet_Membership.DeleteObject(member);
            SaveChanges();
        }
        public void Delete_User(Guid userID)
        {
            aspnet_Users user = db.aspnet_Users.Where(e => e.UserId == userID).Select(e => e).First();
            db.aspnet_Users.DeleteObject(user);
            SaveChanges();
        }
        public void UnlockUser(Guid userID)
        {
            aspnet_Users user = db.aspnet_Users.Where(e => e.UserId == userID).Select(e => e).First();
            user.aspnet_Membership.IsLockedOut = false;
            db.SaveChanges();

        }
        #endregion 
    }
}